<template>
  <div class="friend">
    <div class="friend-box-top">
      <el-card class="box-card">
        <el-form :inline="true" :model="queryFriend" class="queryFriend" ref="queryFriend" :rules="rules">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="queryFriend.username" placeholder="输入需要查找用户名" clearable></el-input>
          </el-form-item>
        </el-form>

        <el-button style="float: right;" type="primary" plain @click="queryFriendList('queryFriend')">查找</el-button>
      </el-card>
    </div>
    <el-divider>好友申请信息</el-divider>
    <div class="friend-box-middle">
      <div v-for="item in addFriendMsg" :key="item.id" class="box-card-middle">
        <img :src="item.avatar"
             class="image">
        <span>申请人:&nbsp&nbsp&nbsp{{ item.username }}</span>
        <br>
        <span>申请信息:&nbsp&nbsp&nbsp&nbsp{{ item.msg }}</span>
        <br>
        <el-button type="text" class="agreeButton" @click="agreeOrRefuse(item.userId,1)">
          {{ '同意' }}
        </el-button>
        <el-button type="text" class="refuseButton" @click="agreeOrRefuse(item.userId,2)">
          {{ '拒绝' }}
        </el-button>
      </div>
    </div>
    <el-divider>搜索列表</el-divider>
    <div class="friend-box-bottom">
      <div v-for="item in friendListData" :key="item.id" class="box-card-bottom">
        <img :src="item.avatar"
             class="image">
        <span class="username">{{ item.username }}</span>
        <el-button type="text" class-="addUserButton" @click="addFriendMethod(item.userId)" :disabled="item.have===1">
          {{ item.have === 1 ? '已添加' : '添加好友' }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {friendListByUserName, addFriend, listByNoPage, agreeFriendMsg} from '@/api/friend/index'

export default {
  computed: {
    monitor() {
      return this.$store.state.addFriendMsg
    }
  },
  name: "index",
  data() {
    return {
      queryFriend: {
        username: ''
      },
      rules: {
        username: [
          {
            required: true, message: '请输入用户名', trigger: 'blur'
          },
          {
            required: true,
            transform: (value) => value && value.trim(),
            message: '用户名不能全为空格',
            trigger: 'blur'
          }
        ]
      },
      friendListData: [],
      addFriend: {
        friendId: '',
        msg: ''
      },
      addFriendMsg: {}
    }
  },
  methods: {
    //查询好友列表
    queryFriendList(val) {
      this.$refs[val].validate(valid => {
        if (valid) {
          friendListByUserName(this.queryFriend.username).then(res => {
            if (res.code === 200) {
              this.friendListData = res.data
            }
          }).catch(error => {

          })
        }
      })
    },
    //添加好友
    addFriendMethod(id) {
      this.$prompt('请输入备注信息', '好友申请备注', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true
      }).then(({value}) => {
        this.addFriend.friendId = id
        this.addFriend.msg = value
        addFriend(this.addFriend).then(res => {
          if (res.code === 200) {
            this.$message.success("发送好友申请成功")
            this.listByNoPageMethod()
          }
        }).catch(error => {

        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消添加'
        });
      });
    },
    //查询好友申请列表信息
    listByNoPageMethod() {
      listByNoPage().then(res => {
        if (res.code === 200) {
          this.addFriendMsg = res.data
        }
      }).catch(error => {

      })
    },
    //同意或拒绝好友申请
    agreeOrRefuse(friend, code) {
      let params = {
        friendId: friend,
        agree: code
      }
      agreeFriendMsg(params).then(res => {
        if (res.code === 200) {
          this.$message.success("操作成功")
          this.listByNoPageMethod()
        }
      }).catch(error => {

      })
    },
    //语音播报
    /**
     * @param sentence：要说的句子
     * @param pitch: 音调，取值范围(0 - 2) 默认值：1
     * @param rate: 语速，取值范围(0.1 - 10) 默认值：1
     */
    startTextToSpeech(sentence, pitch, rate) {
      const speech = new window.SpeechSynthesisUtterance()
      speech.text = sentence
      speech.rate = rate
      speech.pitch = pitch
      speech.lang = 'zh-CN'
      window.speechSynthesis.cancel()
      window.speechSynthesis.speak(speech)
    }
  },
  mounted() {
    this.listByNoPageMethod()
  },
  destroyed() {

  },
  watch: {
    //监听对方发送的数据
    monitor() {
      // 必须处于聊天的页面才进行刷新
      if (this.$route.path === "/friend" && this.$store.state.addFriendMsg.code === 10004) {
        //新消息来了就退出正在播放的语音
        window.speechSynthesis.cancel()
        this.startTextToSpeech("有人加你好友了,快去看看", 1, 1)
        this.listByNoPageMethod()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.friend {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: auto;
}

.friend-box-top {
  flex: 2;
}

.friend-box-middle {
  margin: 10px;
  flex: 3;
}


.queryFriend {
  display: inline-block;
}

.box-card {
  margin: 10px;
}


.bottom {
  margin-top: 13px;
  line-height: 12px;
}


.friend-box-bottom {
  flex: 5;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  row-gap: 80px;
  justify-items: center;
  overflow: auto;

  .box-card-bottom {
    width: 100px;
    height: 100px;
  }
}

.friend-box-middle {
  flex: 5;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  row-gap: 80px;
  justify-items: center;
  overflow: auto;

  .box-card-middle {
    font-size: 12px;
    width: 100px;
    height: 100px;
    margin-top: 40px;
  }
}

.image {
  width: 100%;
  height: 100%;
}

.username {
  text-align: center;
  display: block;
  width: 80px;
  white-space: nowrap;
  word-wrap: normal;
  text-overflow: ellipsis;
  overflow: hidden
}

.username + .el-button {
  margin-left: 15%;
}

.agreeButton{
  float: left;
}
.refuseButton {
  float: right;
  color: #de7979;
}


</style>